// 创建birds.js
var express = require('express')
var responseApi = express.Router()// 创建一个路由实例



// 中间件功能
responseApi.use(function timeLog(req, res, next) {
    console.log('Time: ', new Date().toLocaleString())
    next()
})
// 访问/responseApi
responseApi.get('/', function (req, res) {
    res.send('访问了responseApi,请求成功，开始返回数据')
})

// 访问/getPlus/:id
responseApi.get('/getTest', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)
    res.json({
        header: headers,
        params: params,
        query: query,
        method: req.method,
        url: req.originalUrl,
        msg: '服务器回复'
    })

})

// 访问/getPlus/:id
responseApi.post('/post100', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)
    res.statusCode = 100
    res.send('respondecode 100')

})


responseApi.get('/get101', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(101, {
        'Connection': 'upgrade',
        'Upgrade': 'websocket',
        'Content-Type': 'application/json'
    })
    res.end('success,101 Switching Protocols')
})

responseApi.get('/get200', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(200, {
        'Content-Type': 'text/plain'
    })
    res.end('success,101 Switching Protocols')
})


responseApi.get('/get300', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(300, {
        'Content-Type': 'application/json'
    })
    res.end('client need redirect')
})

responseApi.get('/get301', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(301, {
        'Content-Type': 'application/json',
        "Location": 'https://www.baidu.com'
    })
    res.end('client need redirect')
})
responseApi.get('/get302', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(302, {
        'Content-Type': 'application/json',
        "Location": 'http://1.15.87.213:9527/gettApi/getTest'
    })
    res.end('client need redirect')
})

responseApi.get('/get303', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(303, {
        'Content-Type': 'application/json',
        "Location": 'http://dt.8yx.com'
    })
    res.end('client need redirect')
})


responseApi.get('/get307', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(307, {
        'Content-Type': 'application/json',
        "Location": 'http://dt.8yx.com'
    })
    res.end('client need redirect')
})
responseApi.get('/get308', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(308, {
        'Content-Type': 'application/json',
        "Location": 'https://www.baidu.com'
    })
    res.end('client need redirect')
})



responseApi.post('/post401', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)
    let authorization = req.headers['authorization']
    console.log('authorization   :----> ', authorization)
    if (authorization) {

        if (authorization.startsWith('basic ') || authorization.startsWith('Basic ') || authorization.startsWith('BASIC ')) {
            let str = authorization.replace('basic ', '').replace('Basic ', '').replace('BASIC ', '');
            console.log('str   :----> ', str)
            var decodeStr = Buffer.from(str, 'base64').toString(); 
            console.log('身份认证解析成功   :----> ', decodeStr)
            // JavaScript
            res.send({
                params: params,
                query: query,
                method: req.method,
                url: req.originalUrl,
                msg: 'authorization string = ' + decodeStr
            })
        }


    } else {
        console.log('尚未通过身份认证  ')
        res.writeHead(401, {
            'Content-Type': 'application/json',
            'WWW-Authenticate': 'Basic realm="Secure Area"',
        })
        res.end('need authen')

    }


})



responseApi.get('/get407', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(407, {
        'Content-Type': 'application/json'
    })
    res.end('need proxy authen')
})

responseApi.get('/get408', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(408, {
        'Content-Type': 'application/json',
        'Retry-After': '0'
    })
    res.end('client request timeout')
})
responseApi.post('/post408', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(408, {
        'Content-Type': 'application/json',
        'Retry-After': '0'
    })
    res.end('client request timeout')
})

responseApi.get('/get421', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(421, {
        'Content-Type': 'application/json',
        "Location": 'https://www.baidu.com'
    })
    res.end('client need redirect')
})


responseApi.get('/get503', function (req, res) {
    // 获取参数
    let headers = req.headers ? JSON.stringify(req.headers) : '';
    let params = req.params ? JSON.stringify(req.params) : '';
    let query = req.query ? JSON.stringify(req.query) : '';
    console.log('params :----> ', params)
    console.log('query   :----> ', query)
    console.log('headers   :----> ', headers)

    res.writeHead(503, {
        'Content-Type': 'application/json',
        'Retry-After': '0'
    })
    res.end('client need retry')
})




module.exports = responseApi